<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
$a = getdate();
$b = date("Y-m-d",mktime(0, 0, 0, $a["mon"], $a["mday"], $a["year"]));
$data["LottoID_n"] = (empty($_data["LottoID"]))?$_data["LottoID"]:$_data["LottoID"];
$data["Bet_Type_n"] = (empty($Bet_Type))?$data["Bet_Type_n"]:$Bet_Type;
$data["PayupDate_x"][0] = (empty($_data["PayupDate_0"]))?date("Y-m-d"):$_data["PayupDate_0"];
$data["PayupDate_x"][1] = (empty($_data["PayupDate_1"]))?date("Y-m-d"):$_data["PayupDate_1"];
if(!empty($_data["GambleID"])){
	$arrGD = getGambleDataByID($_data["GambleID"]);
	$data["PayupDate_x"][0] = $arrGD["GameDate"];
	$data["PayupDate_x"][1] = $arrGD["GameDate"];
}
$intReportType = (int)$_data["ReportType"];

/*if($_data["Types"] != ""){
	$data["StockID_n"] = $_data["StockID"];
	$data["MainID_n"] = $_data["MainID"];
}*/
$data["isPayOff_n"] = 0;
//$data["WagerType_n"] = 0;

if($isLimit){
	$data["PayupDate_x"][0] = getPayupDateOfAccountSearchLimit($data["PayupDate_x"][0], $isSevenDay);
	if($data["PayupDate_x"][0] > $data["PayupDate_x"][1]){
		$GLOBALS["errormessage"] = "Over search date range!!";
		ErrMsgOutputHTML("Error", "", 1);
	}
}else{
	if($data["PayupDate_x"][0] > $data["PayupDate_x"][1]){
		$tmp = $data["PayupDate_x"][1];
		$data["PayupDate_x"][1] = $data["PayupDate_x"][0];
		$data["PayupDate_x"][0] = $tmp;
	}
}

$strSQL = SelectSQL ($data, "`bet_BetList`");// USE INDEX (PayupDate)
if($intReportType == 0){
	$n = "select SubID, MainID, COUNT(*) as totals, SUM(MemStake) as MemStake, SUM(SubIncome) as SubIncome, SUM(SubResult) as SubResult, MemExchangeRate, SubRate, MainRate, SUM(SubRateResult) as SubRateResult, SUM(MainResult) as MainResult, SUM(MainRateResult) as MainRateResult, SUM(MemStake*MemExchangeRate*MainRate) as realMemStake, SUM(IF(MemResult = 0, 0, MemStake)*MemExchangeRate*MainRate*-1*(MainCommission - SubCommission)) as MainComm";
	$strSQL = str_replace("select *",$n,$strSQL);
	$strSQL1 = $strSQL;
	$strSQL .=" and WagerType in (0,2) group by SubID order by MainID, SubID limit 0, 500";
	$strSQL1 .=" and WagerType in (3) group by SubID order by MainID, SubID limit 0, 500";
}elseif($intReportType == 1){
	$n = "select LottoID, Bet_Type, MainID, COUNT(*) as totals, SUM(MemStake) as MemStake, SUM(SubIncome) as SubIncome, SUM(SubResult) as SubResult, MemExchangeRate, SubRate, MainRate, SUM(SubRateResult) as SubRateResult, SUM(MainResult) as MainResult, SUM(MainRateResult) as MainRateResult, SUM(MemStake*MemExchangeRate*MainRate) as realMemStake, SUM(IF(MemResult = 0, 0, MemStake)*MemExchangeRate*MainRate*-1*(MainCommission - SubCommission)) as MainComm";
	$strSQL = str_replace("select *",$n,$strSQL);
	$strSQL1 = $strSQL;
	$strSQL .=" and WagerType in (0,2) group by LottoID, Bet_Type limit 0, 500";
	$strSQL1 .=" and WagerType in (3) group by LottoID, Bet_Type limit 0, 500";
}

//一般帳
//print "<!--$strSQL//-->";
if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Stock Report Error";
	$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

$arrRows = array();
$arrUserID = array();
$arrAccount = array();
while($Rows = PMA_mysql_fetch_object($RS)){
	$arrRows[0][] = $Rows;
	$arrUserID[] = $Rows -> MainID;
	if($intReportType == 0)$arrUserID[] = $Rows -> SubID;
}

//向上補貨帳
if($strSQL1 != ""){
	if(!$RS = PMA_mysql_query($strSQL1, $$_Conn)){
		$GLOBALS["errormessage"] = "Main Agent Report Error";
		$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
		ErrMsgOutputHTML("Error", "", 1);
	}
	while($Rows = PMA_mysql_fetch_object($RS)){
		$arrRows[1][] = $Rows;
		$arrUserID[] = $Rows -> MainID;
	}
}

//取得使用者 Account
if(sizeof($arrUserID) > 0){
	$strSQL = "select ID, Account, Name from `bet_Member` where ID in (".join(",", $arrUserID).")";
	if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
		$GLOBALS["errormessage"] = "Find User Data Error";
		$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
		ErrMsgOutputHTML("Error", "", 1);
	}

	while($Rows = PMA_mysql_fetch_object($RS)){
		$arrAccount[$Rows -> ID] = ($_KSysParam[ReportIsShowName]==0)?$Rows -> Account:$Rows -> Account."<a style='color: #336600'>(".$Rows -> Name.")</a>";
	}
}
?>